/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.skillengine.condition;

import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.skillengine.model.Skill;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;


/**
 * @author ATracer
 * @edited kecimis
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ChainCondition")
public class ChainCondition extends Condition {

    @XmlAttribute(name = "selfcount")
    private int selfCount;
    @XmlAttribute(name = "precount")
    private int preCount;
    @XmlAttribute(name = "category")
    private String category;
    @XmlAttribute(name = "precategory")
    private String precategory;
    @XmlAttribute(name = "time")
    private int time;

    @Override
    public boolean validate(Skill env) {

        if (env.getEffector() instanceof Player && (precategory != null || selfCount > 0)) {
            Player pl = (Player) env.getEffector();

            if (selfCount > 0) {// multicast
                boolean canUse = false;

                if (precategory != null && pl.getChainSkills().chainSkillEnabled(precategory, time)) {
                    canUse = true;
                }

                if (pl.getChainSkills().chainSkillEnabled(category, time)) {
                    canUse = true;
                } else if (precategory == null) {
                    canUse = true;
                }

                if (!canUse)
                    return false;

                if (selfCount <= pl.getChainSkills().getChainCount(pl, env.getSkillTemplate(), category)) {
                    return false;
                } else {
                    env.setIsMultiCast(true);
                }
            } else if (preCount > 0 && selfCount > 0) {
                if (!pl.getChainSkills().chainSkillEnabled(precategory, time)
                        || preCount != pl.getChainSkills().getChainCount(pl, env.getSkillTemplate(), precategory)) {
                    return false;
                }
            } else {// basic chain skill
                if (!pl.getChainSkills().chainSkillEnabled(precategory, time)) {
                    return false;
                }
            }
        }

        env.setChainCategory(category);
        return true;
    }


    /**
     * @return the selfCount
     */
    public int getSelfCount() {
        return selfCount;
    }


    /**
     * @return the category
     */
    public String getCategory() {
        return category;
    }


    /**
     * @return the time
     */
    public int getTime() {
        return time;
    }


}
